package com.controller;

import com.entity.view.MeishiView;
import com.model.enums.ItemType;
import com.service.RecommendationService;
import com.entity.MeishiEntity;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/recommend")
public class RecommendationController {

    @Autowired
    private RecommendationService recommendationService;

    /**
     * 基于用户的美食推荐
     * @param userId 用户ID
     * @param limit 推荐数量
     * @return 推荐美食列表，结构与 /meishi/list 一致
     */
    @GetMapping("/user")
    public R userRecommend(
            @RequestParam("userId") String userId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        List<MeishiView> recommendList = recommendationService.getUserBasedRecommendationViews(userId, page, limit);
        PageUtils pageObj = new PageUtils(recommendList, recommendList.size(), limit, page);
        return R.ok().put("data", pageObj);
    }

    /**
     * 热门美食（按收藏数降序）分页
     */
    @GetMapping("/hot")
    public R hotRecommend(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        List<MeishiView> list = recommendationService.getHotMeishiByCollectionViews(page, limit);
        PageUtils pageObj = new PageUtils(list, list.size(), limit, page);
        return R.ok().put("data", pageObj);
    }

    /**
     * 获取用户相似度矩阵
     * @return 用户相似度矩阵
     */
    @GetMapping("/user-similarity")
    public Map<String, Map<String, Double>> getUserSimilarityMatrix() {
        return recommendationService.getUserSimilarityMatrix(ItemType.MEISHI);
    }
    
    /**
     * 获取AI增强的个性化推荐
     * @param userId 用户ID
     * @param limit 推荐数量
     * @return 包含AI建议的推荐结果
     */
    @GetMapping("/ai-enhanced")
    public R getAIEnhancedRecommendations(
            @RequestParam("userId") String userId,
            @RequestParam(value = "limit", defaultValue = "10") int limit
    ) {
        Map<String, Object> result = recommendationService.getAIEnhancedRecommendations(userId, limit);
        return R.ok().put("data", result);
    }
}